/***************************************************************************************
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
*
* NEMU is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include <isa.h>
#include <common.h>

#define INTR_TIMER 0x8000000000000007  // for riscv64

word_t isa_raise_intr(word_t NO, vaddr_t epc) {
    cpu.csr.mepc.value = epc;
    cpu.csr.mcause.value = NO;
    cpu.csr.mstatus.MPIE = cpu.csr.mstatus.MIE;
    cpu.csr.mstatus.MIE = 0;
    return cpu.csr.mtvec.value;
}

word_t isa_query_intr() {
    if (cpu.csr.mstatus.MIE && cpu.csr.mie.MTIE && cpu.csr.mip.MTIP) {
        return INTR_TIMER;
    }
    return INTR_EMPTY;
}
